package com.cedifie.lendapp.ui.steps.adapter

import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cedifie.lendapp.R
import com.cedifie.lendapp.http.HttpHelper
import com.cedifie.lendapp.utils.decoration.LinearSpacingItemDecoration
import com.cedifie.lendapp.utils.dialog.StepCommonPopup
import com.cedifie.lendapp.utils.event.CedEventType
import com.cedifie.lib_baseadapter.CEDRecyclerViewAdapter
import com.cedifie.lib_baseadapter.CEDViewHolderHelper
import com.lib.base.utils.DensityUtils
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/4/18 10:43
 * @Annotation：流程 - 联系人适配器
 */
class CedStepContactAdapter(recyclerView: RecyclerView?) : CEDRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_step_contact_brand) {

    var parentView: ViewGroup? = null

    /**
     * 选择联系电话监听
     */
    private var selectContactMobileobileListsner: ISelectMobileListener? = null
    fun setOnContactSelectMobileListener(listener: ISelectMobileListener){
        selectContactMobileobileListsner = listener
    }

    override fun fillData(helper: CEDViewHolderHelper, position: Int, model: JSONObject) {
        val tvBrandTitle = helper.getTextView(R.id.tvBrandTitle)
        var showContactIndex = String.format(mContext.getString(R.string.process_contact_index),"${position+1}")
        if (position > 0){
            showContactIndex = "$showContactIndex ${mContext.getString(R.string.process_contact_different)}"
        }
        tvBrandTitle.text = showContactIndex

        val recyclerView = helper.getView<RecyclerView>(R.id.recyclerView)
        recyclerView.setHasFixedSize(true)
        recyclerView.isNestedScrollingEnabled = false
        recyclerView.isFocusableInTouchMode = false //不需要焦点
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        if (recyclerView.itemDecorationCount == 0){
            val topDecoration = DensityUtils.dp2px(14f)
            recyclerView.addItemDecoration(LinearSpacingItemDecoration(0,0,0,topDecoration))
        }
        val adapter = CedStepCommonAdapter(recyclerView)
        adapter.typeFaceStyle = Typeface.NORMAL
        recyclerView.adapter = adapter
        recyclerView.itemAnimator = DefaultItemAnimator()
        adapter.setOnItemChildClickListener { _, childView, childPosition ->
            val childModel = adapter.getItem(childPosition)
            when(childModel.optString("action")){
                "SELECT",
                "CONTACT_GROUP_RELATION" -> {//选择
                    val dataList = arrayListOf<JSONObject>()
                    val dataArray = childModel.optJSONArray("values")
                    if (dataArray != null) {
                        for (i in 0..<dataArray.length()) {
                            dataList.add(dataArray.optJSONObject(i))
                        }
                    }
                    showPopupwindow(childView, dataList) { data ->
                        //选择的Other需要设置为空
                        data.put("otherValue","")
                        childModel.put("selectItem", data)
                        adapter.notifyItemChanged(childPosition)

                        val eventType = String.format(CedEventType.CLICK.CED_CLICK_STEP_XX_SELECT,"${model.optString("stepTitle")}_${childModel.optString("paramName")}_${position+1}")
                        val subRemark = "${data.optString("value")}$$${data.optString("duration")}"
                        HttpHelper.addCedEventAction(eventType, subRemark)
                    }
                }
                "CONTACT_GROUP_PHONE" -> {//联系电话
                    if (selectContactMobileobileListsner != null){
                        selectContactMobileobileListsner?.onSelectMobileClick(position,childModel)
                    }
                }
            }
        }

        val dataArray = model.optJSONArray("dataList")
        val dataList = arrayListOf<JSONObject>()
        if (dataArray != null) {
            for (i in 0..<dataArray.length()){
                val item = dataArray.optJSONObject(i)
                item.put("stepTitle",model.optString("stepTitle"))
                item.put("stepType","CONTACT")
                item.put("stepItemIndex",position+1)
                dataList.add(item)
            }
        }
        adapter.data = dataList
    }

    /**
     * choose Mobile
     */
    interface ISelectMobileListener{
        fun onSelectMobileClick(parentPosition: Int,childModel: JSONObject)
    }

    fun interface IChildItemSelectListener{
        fun onChildSelectClick(data: JSONObject)
    }

    /**
     * 选择选项
     */
    private fun showPopupwindow(view: View, dataList: ArrayList<JSONObject>, listener: IChildItemSelectListener){
        val selectPopup = StepCommonPopup(mContext)
        selectPopup.setWindowWidth(view.width + DensityUtils.dp2px(10f))
        selectPopup.setSelectItemListener(object : StepCommonPopup.IItemClickListener {
            //完成
            override fun onConfirmAction(data: JSONObject?) {
                data?.let {
                    listener.onChildSelectClick(it)
                }
            }
            //关闭监听
            override fun onDismissListener() {
            }
        })
        parentView?.let {
            selectPopup.show(view, it)
            selectPopup.setBrandList(dataList)
        }
    }
}